//
//  YAYINavigationController.m
//  YAYIDoctor
//
//  Created by hxp on 16/4/28.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "XMGNavigationController.h"

#import "UIImage+VR.h"
#import "UIBarButtonItem+Item.h"

@interface XMGNavigationController () <UIGestureRecognizerDelegate>

@end


@implementation XMGNavigationController



- (void)setNavBarAppearence
{

//    // 设置导航栏默认的背景颜色
//    [WRNavigationBar wr_setDefaultNavBarBarTintColor:[UIColor whiteColor]];
////    // 设置导航栏所有按钮的默认颜色
//    [WRNavigationBar wr_setDefaultNavBarTintColor:[UIColor whiteColor]];
////    // 设置导航栏标题默认颜色
//    [WRNavigationBar wr_setDefaultNavBarTitleColor:[UIColor blackColor]];
////    // 统一设置状态栏样式
//    [WRNavigationBar wr_setDefaultStatusBarStyle:UIStatusBarStyleLightContent];
    // 如果需要设置导航栏底部分割线隐藏，可以在这里统一设置
//    [WRNavigationBar wr_setDefaultNavBarShadowImageHidden:YES];
    

    [self.navigationBar setBackgroundImage:[UIImage imageWithColor:NAVColor] forBarPosition:UIBarPositionTopAttached barMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationBar setShadowImage:[UIImage imageWithColor:RGB(220, 220, 220) size:CGSizeMake(kScreenWidth, 0.5)]];
    [self.navigationBar setShadowImage:[UIImage new]];
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = RGB(220, 220, 220);
    [self.navigationBar addSubview:lineView];
    lineView.frame = CGRectMake(0, self.navigationBar.height - 0.5, kScreenWidth, 0.5);

    self.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor], NSFontAttributeName:VRFont(18)};
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1.创建全屏手势
//    self.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 50;
//    UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
//
//    // 控制手势什么时候触发
//    edgePan.delegate = self;
//    edgePan.edges = UIRectEdgeLeft;
//    // 全屏滑动返回
//    [self.view addGestureRecognizer:edgePan];
//
//    // 2.禁止边缘手势
//    self.interactivePopGestureRecognizer.enabled = NO;
    
    //1.设置阴影颜色
    //    self.navigationBar.layer.shadowColor = RGB(252, 86, 90).CGColor;
    
    //2.设置阴影偏移范围
    //    self.navigationBar.layer.shadowOffset = CGSizeMake(0, 2);
    //3.设置阴影颜色的透明度
    //    self.navigationBar.layer.shadowOpacity = 0.53;
    //4.设置阴影半径
    //    self.navigationBar.layer.shadowRadius = 4;
    //5.设置阴影路径
    //    self.navigationBar.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.navigationBar.bounds].CGPath;
    
    [self setNavBarAppearence];
}

/**
 * 可以在这个方法中拦截所有push进来的控制器
 */
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    
    if (self.childViewControllers.count > 0) { // 如果push进来的不是第一个控制器
        
        viewController.fd_interactivePopDisabled = NO;
        viewController.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 50;
        [self changeLeftBarButtonItem:viewController];
        //        UIBarButtonItem *temporaryBarButtonItem = [[UIBarButtonItem alloc] init];
        //        temporaryBarButtonItem.title = @"dd";
        //        viewController.navigationItem.backBarButtonItem = temporaryBarButtonItem;
        
        // 隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
    }
    // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
    [super pushViewController:viewController animated:animated];
}

- (void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers animated:(BOOL)animated
{
    [self changeLeftBarButtonItem:[viewControllers lastObject]];
    [super setViewControllers:viewControllers animated:animated];
}

- (void)changeLeftBarButtonItem:(UIViewController *)viewController
{
    //统一设置返回按钮
    UIBarButtonItem *backItem = [UIBarButtonItem backItemWithImage:[UIImage imageNamed:@"back_normal"] highImage:[UIImage imageNamed:@"back_normal"] target:self action:@selector(back) norColor:MAINColor highColor:RGB(120, 120, 120) title:@"返回"];
    
    //创建UIBarButtonSystemItemFixedSpace
    UIBarButtonItem * spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -0;
    
    viewController.navigationItem.leftBarButtonItems = @[spaceItem, backItem];
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}


#pragma mark - UIGestureRecognizerDelegate
// 控制手势是否触发
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([gestureRecognizer isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
        // 根控制器的时候
        return self.childViewControllers.count != 1;
    }

    return YES;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

- (UIViewController *)childViewControllerForStatusBarStyle{
    return self.topViewController;
}


@end

